
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>


<?php
// dictionary.ob.php
// hladek.m
// Start working 2013 - 02 - 25
// separated from administrationpanel.php on 2013-03-17

abstract
class Dictionary
{
	var $datatable;
	var $dictName = 'Dictionary Items';
	var $columns; 	// sql column name as row index, UI name as 0 value
					// string width in percents as 1st value
					// prefix to CSS / jQ class id as 2rd value
					// type of input in form as 3th value
	var $readQuery; // IMPORTANT: always there is a column named 'id' with row uniqe id
	var $dictelem = array();	// elements of dictionary
	var $idchng = array();	// array of id edited and returned by $_POST

	abstract protected function initproperties ();
	abstract protected 	function saverow($id);

	public
	function __construct ()
	{
		$this->initproperties();
		$this->datatable = mysql_query($this->readQuery);

		while($row = mysql_fetch_array($this->datatable, MYSQL_ASSOC))
		{
			$this->dictelem[$row['id']] = $row;
		}; 
		
		if(empty($_POST))
		{
			$_SESSION[$this->dictName] = time();
		}
		else
		{
			foreach($_POST as $i => $val){
				$idx = explode('|', $i);
				if ($idx[0] == $this->dictName && $this->dictelem[$idx[2]][$idx[1]] != $val)
				{
					$this->dictelem[$idx[2]][$idx[1]] = $val;
					$this->idchng[] = $idx[2];
				}
			};
			$this->idchng = array_unique($this->idchng);
			foreach($this->idchng as $i) {
				$this->saverow($i);
			}
		}; // else - $_POST not empty
	}


	public
	function show ()
	{
		$html = '<form action="'."administrationpanel.php".'" method="POST" class="dict" id="'.$this->dictName.'">'."\n";
		$html .= '<div class="title">'."\n";
		$html .= '<h2 class="name">'.$this->dictName.'</h2>'."\n";
		if(NULL == $this->dictelem)
		{
			//if there is nothing to display
			$html .= 'No '.$this->dictName.' defined'."\n";
			return $html."</div>\n</form>\n";
		};
		$html .= '<div class="actions">'."\n";
		$html .= '<p class="action_save">Save</p>'."\n";
		$html .= '<p class="action_edit">Edit</p>'."\n";
		$html .= "</div> \n </div> \n"; //classes: actions and title
		$html .= '<table class="dict">'."\n";
		$html .= '<tr class="heading">'."\n";
		foreach($this->columns as $i => $ui)
		{
			$html .= '<th class="'.$ui[2].'col" style="width : '.$ui[1].'%">';
			$html .= $ui[0]."</th> \n";
		};
		$html .= "</tr> \n";

		foreach ($this->dictelem as $j => $row)
		{
			$html .= '<tr class="item">'."\n";
			foreach($this->columns as $i => $ui)
			{
				$html .= '<td class="'.$ui[2].'col" style="width : '.$ui[1].'%">';
				if('' == $ui[2])
				{
					$html .= '<input type="'.if_empty(@$ui[3],'text').'" name="';
					$html .= $this->dictName.'|'.$i.'|'.$row['id'];
					$html .= '" value="'.$row[$i].'" disabled="true"';
					if('checkbox' == @$ui[3] & '0' <> $row[$i])
					{
						$html .= ' checked ';
					};
					$html .= '/>' ;
				}
				else
				{
					$html .= $row[$i];
				} ;
				$html .= "</td> \n";
			};
			$html .= "</tr> \n";
		};
		$html .= "</table> \n </form> \n";
		$html .= '<script>
		$(document).ready(function(){
		$("form#'.$this->dictName.' .action_edit").click(function()
		{
		$("form#'.$this->dictName.' td.col input").attr("disabled",false);
		});
		$("form#'.$this->dictName.' .action_save").click(function()
		{
			document.getElementById("'.$this->dictName.'").submit();
		});
		});
		</script>'."\n";
		return $html."\n";
	}


}

?>